// pages/login/login.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    phonePles: false,
    pass: '',
    passPles: false,
    okaySataus: false,
    getCode: 0
  },
  phoneInput() {
    console.log(22);
    if (this.data.phone === '') {
      this.setData({
        phonePles: true
      })
    } else if(!/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(this.data.phone)) {
      this.setData({
        phonePles: true
      })
      } else {
      this.setData({
        phonePles: false
      })
    }
  },
  passInput() {
    console.log(33);
    if (this.data.pass === '') {
      this.setData({
        passPles: true
      })
    } else {
      this.setData({
        passPles: false
      })
    }
  },
  clickClode(){
    this.phoneInput()
    if (!this.data.phonePles) {
      wx.request({
        url: app.globalData.baseURL +  '/sms/codes/'+ this.data.phone,
        method: 'GET',
        timeout: 5000,
        success: () => {
          this.setData({
            okaySataus: true,
            getCode: 60
          })
          setTimeout(()=>{
            this.setData({
              okaySataus: false
            })
          }, 500)
          this.downTime()
        },
      })
    }
  },
  downTime() {
    setTimeout(() => {
      this.setData({
        getCode: this.data.getCode - 1
      })
      if (this.data.getCode !== 0) {
        this.downTime()
      }
    }, 1000)
  },
  loginFn(){
    this.phoneInput()
    this.passInput()
    if (!this.data.passPles && !this.data.phonePles) {
      console.log('Okay');
      wx.request({
        url: app.globalData.baseURL + '/authorizations',
        method: 'POST',
        data: {
          code: this.data.pass,
          mobile: this.data.phone
        },
        success: (result) => {
          console.log(result);
          if (result.data.message === "OK") {
            wx.setStorage({
              key: 'tokens',
              data: result.data.data,
              success: ()=> {
                app.getTokens(true)
                wx.switchTab({
                  url: '/pages/profile/profile'
                })
              }
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})